package com.wink.admin.config;

import cn.hutool.core.util.StrUtil;
import com.wink.admin.utils.DateUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转换处理
 *
 * @author donaldhan
 */
@Configuration
public class DateConverter implements Converter<String, Date> {

    /**
     * 自动时间转换
     *
     * @param s 时间字符串，能解析为Date
     * @return {@link Date}
     */
    @Override
    public Date convert(String s) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATA_TIME_S_FORMAT);
        Date date = null;
        try {
            if (!StrUtil.isBlank(s)) {
                date = sdf.parse(s);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}